package com.briup.mt.web.controller;

import com.briup.mt.bean.Counsel;
import com.briup.mt.bean.Dataset;
import com.briup.mt.service.IGreenHouseMessageService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yxy
 * @version 1.0
 * @date 2020/12/16 22:28
 */
@RestController
@Validated
@RequestMapping("/greenhouse_message")
public class GreenHouseMessageController {
    @Resource
    private IGreenHouseMessageService iGreenHouseMessageService;
    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "application/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态信息", paramType = "query")
    })
    public void download(HttpServletResponse response, String status) throws Exception {
        // 2. 将excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "序号", "问题", "提问时间", "回复","回复时间","处理状态","专家Id","用户Id" };
        // 2.2 属性
        String[] fieldList = new String[] { "id", "question", "questionTime", "replay","replayTime",
                "status", "professorId", "userId"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<Counsel> list= iGreenHouseMessageService.query(status);
        for (Counsel counsel : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", counsel.getId());
            map.put("question", counsel.getQuestion());
            map.put("qusetionTime", counsel.getQuestionTime());
            map.put("replay", counsel.getReplay());
            map.put("replyTime", counsel.getReplayTime());
            map.put("status", counsel.getStatus());
            map.put("professorId", counsel.getProfessorId());
            map.put("userId", counsel.getUserId());
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "counsel_list";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }

    @GetMapping("query")
    public Message query(String status){
        List<Counsel> list = iGreenHouseMessageService.query(status);
        return MessageUtil.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Counsel info){
        iGreenHouseMessageService.saveOrUpdate(info);
        return MessageUtil.success("操作成功");
    }

    @DeleteMapping("deleteByID")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户编号",required =true, paramType = "query"),})

    public Message deleteByID(@NotNull long id){

        iGreenHouseMessageService.deleteById(id);
        return MessageUtil.success("操作成功");
    }
}
